package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceimpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
@Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO getturnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();

        list.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            list.add(begin);
        }
        List<Object> arrayList = new ArrayList<>();
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 :turnover;
            arrayList.add(turnover);
        }

        return TurnoverReportVO
                .builder().dateList(  StringUtils.join(list,","))
                .turnoverList(StringUtils.join(arrayList,","))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();

        list.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            list.add(begin);
        }
        //每天新增用户数量
        List<Integer> NewUserlist = new ArrayList<>();
        //用户总数
        List<Integer> TotalUserlist = new ArrayList<>();

        for (LocalDate date : list) {
            LocalDateTime Userbegin = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime Userend = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("end",Userend);//先计算出end的时间 然后的得到总的用户数量
            //然后再通过调用mapper查出对应的数据
           Integer TotalUser = userMapper.countByMap(map);
            TotalUserlist.add(TotalUser);
            map.put("begin",Userbegin);
            Integer NewUser = userMapper.countByMap(map);
            NewUserlist.add(NewUser);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(list,","))
                .totalUserList(StringUtils.join(TotalUserlist,","))
                .newUserList(StringUtils.join(NewUserlist,","))
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();

        list.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            list.add(begin);
        }
        //第一条统计所有
        List<Integer> arrayList = new ArrayList<>();
        //第二条统计已完成
        List<Integer> arrayList1 = new ArrayList<>();
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
          Integer orderCount = getOrdercount(beginTime,endTime,null);
          Integer allCount = getOrdercount(beginTime,endTime,5);
          arrayList.add(orderCount);
          arrayList1.add(allCount);
        }
        //总订单数
        Integer allCount = arrayList.stream().reduce(Integer::sum).get();
        //有效订单数
        Integer usefulCount = arrayList.stream().reduce(Integer::sum).get();
        //计算订单完成率
       double use =  0.0;
        if(use != 0){
         use =  usefulCount / allCount;
        }
        return OrderReportVO.builder()
                 .dateList(StringUtils.join(list,","))
                 .orderCountList(StringUtils.join(arrayList,","))
                 .validOrderCountList(StringUtils.join(arrayList1,","))
                 .totalOrderCount(allCount)
                 .validOrderCount(usefulCount)
                 .orderCompletionRate(use)
                 .build();

    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> top = orderMapper.getTop(beginTime, endTime);
        List<String> list = top.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(list,",");
        List<Integer> number = top.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(number,",");
        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }

    @Override
    public void exportDate(HttpServletResponse response) {
        
    }

    private Integer getOrdercount(LocalDateTime begin,LocalDateTime end,Integer status){
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status", status);
       return orderMapper.countByMap(map);

    }
}
